﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CashDriver;
using CashResource;
using CashCommon;
using Cash.Model;
using System.Drawing.Printing;


namespace Cash
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            this.Text = CommonText.MainForm_Title;
        }

        // События формы
        private void frmMain_Load(object sender, EventArgs e)
        {
            // Назначение событий пунктам меню
            AssignEventToMenu();

            // Открываем окно с документами на сегодняшний день
            MainMenuEvents.OnDocumentsFormShow(null, null);
            this.FormRefresh();
        }

        #region Helper

        /// <summary>
        /// Для пунктов меню назначаются обработчики событий
        /// </summary>
        private void AssignEventToMenu()
        {
            this.tsmiFileExit.Click += new EventHandler(MainMenuEvents.OnMainFormClose);
            this.tsmiFileConnect.Click += new EventHandler(MainMenuEvents.OnLoginFormShow);
            this.tsmiDocAccountOperation.Click += new EventHandler(MainMenuEvents.OnFormAccountShow);
            this.tsmiDocCashOperation.Click += new EventHandler(MainMenuEvents.OnFormCashShow);
            this.tsmiDocsList.Click += new EventHandler(MainMenuEvents.OnDocumentsFormShow);
            this.tsmiHelpAbout.Click += new EventHandler(MainMenuEvents.OnFormAboutCashShow);
            this.tsmiFileConnectClose.Click += new EventHandler(MainMenuEvents.OnConnectionClose);
            this.tsmiHelpHelp.Click +=new EventHandler(MainMenuEvents.OnHelpOpen);
        }

        /// <summary>
        /// Обновляются значения и/или доступность элементов формы
        /// </summary>
        public void FormRefresh()
        {
            bool inConnect = Builder.DB.IsActive();


            #region Доступность элементов меню при подключнии/отключении к базе

            foreach (ToolStripMenuItem item in new Collection<ToolStripMenuItem>() { tsmiDocuments, tsmiFileConnectClose })
                item.Enabled = inConnect;

            #endregion

            #region Отображение данных о текущем соединении

            if (Builder.DB.SqlConnection != null && Builder.DB.SqlConnection.State == ConnectionState.Open)
            {
                tsslServerName.Text = String.Format("{0} {1}", CommonText.MainFormStatusBar_Server, Builder.DB.ServerName);
                tsslDataBaseName.Text = String.Format("{0} {1}", CommonText.MainFormStatusBar_Base, Builder.DB.DbName);
                tsslLogin.Text = String.Format("{0} {1}/{2}", CommonText.MainFormStatusBar_User, Builder.DB.UserHost, Builder.DB.Login +" ("+CashEmployer.Inst.C_FIO +")");
            }
            else
            {
                tsslServerName.Text = "";
                tsslDataBaseName.Text = "";
                tsslLogin.Text = "";
            }

            #endregion

        }

        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            Tools.GetHelp("inf_Main_Form", HelpNavigator.Topic);
        }

        private void PrinterInit(PrintPageEventHandler printPageEventHandler)
        {
            printDocument = new PrintDocument();
            printDocument.DocumentName = CommonText.PrintDocCashCopyTitle;
            if (printPageEventHandler != null)
                printDocument.PrintPage += printPageEventHandler;

            printDialog = new PrintDialog();
            printDialog.Document = printDocument;

            printPreviewDialog = new PrintPreviewDialog();
            printPreviewDialog.Document = printDocument;

            pageSetupDialog = new PageSetupDialog();
            pageSetupDialog.Document = printDocument;
            pageSetupDialog.PageSettings.Landscape = true;
        }

        public void Print(PrintPageEventHandler printPageEventHandler)
        {
            PrinterInit(printPageEventHandler);
            printDocument.Print();
        }

        public void PreviewDialogShow(PrintPageEventHandler printPageEventHandler)
        {
            PrinterInit(printPageEventHandler);
            printPreviewDialog.ShowDialog();
        }

        public void PrintDialogShow()
        {
            PrinterInit(null);
            printDialog.ShowDialog();
        }


        private PrintDialog printDialog;
        public PrintPreviewDialog printPreviewDialog;
        private PageSetupDialog pageSetupDialog;
        private PrintDocument printDocument;
    }

}
